导入第三方库到目标

介绍

如先前在第三方库中所述,较新版本的CMake允许您使用导入的ALIASimported目标链接第三方库。

本教程中的文件树如下:

$ tree
.
├── CMakeLists.txt
├── main.cpp

环境版本

  • CMake v3.5+

  • 安装在默认位置的boost库

解析

导入目标

Imported targets是FindXXX模块导出的只读目标。

在CMake命令中包含boost这个库:

  target_link_libraries( imported_targets
      PRIVATE
          Boost::filesystem
  )
作用是自动链接Boost

filesystem和Boost :: system库,同时还包括Boost 头文件目录。

编译例子

$ mkdir build

$ cd build/

$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Boost version: 1.58.0
-- Found the following Boost libraries:
--   filesystem
--   system
boost found
-- Configuring done
-- Generating done
-- Build files have been written to: /data/code/01-basic/K-imported-targets/build

$ make
Scanning dependencies of target imported_targets
[ 50%] Building CXX object CMakeFiles/imported_targets.dir/main.cpp.o
[100%] Linking CXX executable imported_targets
[100%] Built target imported_targets


$ ./imported_targets
Hello Third Party Include!
Path is not relative

results matching ""

    No results matching ""